package com.example.spring_config.util;

import com.example.spring_config.bean.ResultInfo;
import com.example.spring_config.mapper.Permission;

import java.util.Map;

public class CheckUtil {
    private CheckUtil(){}

    /**
     * 验证父权限是否被禁用
     * @param permissionMap
     * @param url
     * @return
     */
    public static boolean checkPermission(Map<String, Permission> permissionMap, String url){
        for (String s : permissionMap.keySet()) {
            Permission permission = permissionMap.get(s);
            if(permission.getUrl().contains(url)){
                if(permission.getStatus()==0 || !checkParent(permissionMap,permission.getId())){
                    return false;
                }else {
                    return true;
                }
            }
        }
        return true;
    }

    // 验证父权限权限是否被禁用
    private static boolean checkParent(Map<String, Permission> permissionMap, String id){
/*        if(permissionMap.get(id)==null){
            return true;
        }*/
        if(permissionMap.get(id).getStatus()==0){
            return false;
        }else {
            Permission permission = permissionMap.get(permissionMap.get(id).getPId());
            if(permission!=null && permission.getStatus()==0){
                return false;
            }else if(permission!=null && permission.getStatus()==1){
                return checkPermission(permissionMap,permission.getPId());
            }else {
                return true;
            }
        }
    }
}
